package bsf.baseservice.businessmq.dal;

import bsf.baseservice.businessmq.model.tb_datanode_model;
import bsf.db.DbConn;
import bsf.util.ConvertUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class tb_datanode_dal
{
	public ArrayList<tb_datanode_model> list(DbConn PubConn, java.util.ArrayList<Integer> datanodepartitions)
	{

			java.util.ArrayList<tb_datanode_model> rs = new java.util.ArrayList<tb_datanode_model>();
			if (datanodepartitions.size() > 0)
			{
				StringBuilder stringSql = new StringBuilder();
				stringSql.append(String.format("select s.* from tb_datanode s WITH(NOLOCK) where datanodepartition in (%1$s)",
						StringUtils.strip(StringUtils.join(datanodepartitions.toArray(),','),",")));
				List<Map<String, Object>> ds = PubConn.executeList(stringSql.toString(),new Object[]{});
				if (ds != null && ds.size() > 0)
				{
					for (Map<String, Object> dr : ds)
					{
						rs.add(createModel(dr));
					}
				}
			}
			return rs;

	}

	public tb_datanode_model get2(DbConn PubConn, int datanodepartition)
	{
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_datanode s WITH(NOLOCK) where s.datanodepartition=?");
		List<Map<String, Object>> ds = PubConn.executeList(stringSql.toString(), new Object[]{datanodepartition});
			if (ds != null && ds.size() > 0)
			{
				return createModel(ds.get(0));
			}
			return null;

	}



	public tb_datanode_model createModel(Map<String, Object> dr)
	{
		tb_datanode_model o = new tb_datanode_model();

		//
		if (dr.containsKey("id"))
		{
			o.id= ConvertUtil.objToInt(dr.get("id"));
		}
		//
		if (dr.containsKey("datanodepartition"))
		{
			o.datanodepartition= ConvertUtil.objToInt(dr.get("datanodepartition"));
		}
		//
		if (dr.containsKey("serverip"))
		{
			o.serverip= ConvertUtil.nullToStr(dr.get("serverip"));
		}
		//
		if (dr.containsKey("username"))
		{
			o.username= ConvertUtil.nullToStr(dr.get("username"));
		}
		//
		if (dr.containsKey("password"))
		{
			o.password= ConvertUtil.nullToStr(dr.get("password"));
		}
		return o;
	}
}